﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Context;
using NUnit.Framework;

namespace Cos.Core.NHibernate.Tests
{
    public class InMemoryFixtureBase
    {
        protected SessionSource SessionSource { get; set; }

        protected ISession Session { get; private set; }

        protected PersistenceModel model;

        public InMemoryFixtureBase(PersistenceModel model)
        {
            this.model = model;
        }

        [TestFixtureSetUp]
        public void SetupContext()
        {
            var cfg = Fluently.Configure()
                .Database(SQLiteConfiguration.Standard.ShowSql().InMemory)
                .ExposeConfiguration(c => c.SetProperty("current_session_context_class", "thread_static"));
            SessionSource = new SessionSource(cfg.BuildConfiguration().Properties, new TestModel());
            
            this.Session = this.SessionSource.CreateSession();
            this.SessionSource.BuildSchema(this.Session);
            CurrentSessionContext.Bind(this.Session);
        }

        [TestFixtureTearDown]
        public void TearDownContext()
        {
            this.Session.Close();
            this.Session.Dispose();
        }
    }
}
